package com.snail.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.snail.model.pojo.user.SysUser;
import com.snail.model.vo.user.UpdatePasswordVO;
import com.snail.model.vo.user.UserAssignRoleVO;
import com.snail.result.Result;
import com.snail.service.SysUserRoleService;
import com.snail.service.SysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 蔡徐坤
 * @Date 2025/8/14 10:32
 * @Description: TODO
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 根据用户ID获取用户信息
     */
    @GetMapping("/getUserById/{id}")
    public Result<SysUser> getUserById(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getUserById(id);
        return Result.success(sysUser);
    }

    /**
     * 获取当前登录用户的信息
     */
    @GetMapping("/getUser")
    public Result<SysUser> getUser() {
        SysUser sysUser = sysUserService.getUser();
        return Result.success(sysUser);
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/findAllUsers")
    public Result<List<SysUser>> findAllUsers() {
        return Result.success(sysUserService.findAllUsers());
    }


    /**
     * 更新用户信息
     */
//    @SaCheckRole("admin")
    @PutMapping("/updateUser")
    public Result<String> updateUser(@RequestBody SysUser sysUser) {
        sysUserService.updateUser(sysUser);
        return Result.success("更新成功");
    }

    /**
     * 修改密码(用于用户个人信息自己修改密码)
     */
    @SaCheckRole("user")
    @PutMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody UpdatePasswordVO userVO) {
        sysUserService.updatePassword(userVO);
        return Result.success("密码修改成功");
    }

    /**
     * 重置密码为默认密码（123456）
     */
    @SaCheckRole("admin")
    @PutMapping("/resetPassword/{id}")
    public Result<String> resetPassword(@PathVariable Long id) {
        sysUserService.resetPassword(id);
        return Result.success("密码重置成功");
    }

    /**
     * 删除用户（逻辑删除）
     */
    @DeleteMapping("/deleteUser/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        sysUserService.deleteUser(id);
        return Result.success("删除成功");
    }

    /**
     * 分配角色给用户（覆盖旧绑定）
     */
    @PostMapping("/addUserToRole")
    public Result<String> addUserToRole(@RequestBody UserAssignRoleVO request) {
        sysUserRoleService.addUserToRole(request.getUserId(), request.getRoleIds());
        return Result.success("角色分配成功");
    }

    /**
     * 查询用户角色 ID 列表
     */
    @GetMapping("/getRoleIds")
    public Result<List<Long>> getRoles(@RequestParam Long userId) {
        List<Long> roleIdsByUserId = sysUserRoleService.getRoleIdsByUserId(userId);
        return Result.success(roleIdsByUserId);
    }

    /**
     * 批量查询所有用户的角色ID映射
     */
    @GetMapping("/getAllUserRoles")
    public Result<Map<Long, List<Long>>> getAllUserRoles() {
        Map<Long, List<Long>> userRoleMap = sysUserRoleService.getAllUserRoleMap();
        return Result.success(userRoleMap);
    }
}
